<template>
	<view>
		<u-sticky :enable="stickyEnable">
			<view class="padding-sm bg-white">
				<u-search placeholder="搜索名称" v-model="page.cond.name" @search="search" @custom="search"></u-search>
			</view>
		</u-sticky>
		<tui-list-view v-for="(item,index) in result.records" :key="index">
			<tui-swipe-action :actions="actions" :params="item" @click="actionHandler">
				<template v-slot:content>
					<tui-list-cell :arrow="true" @click.native="childList(item)">
						<view class="flex margin-right align-center">
							<view class="flex-sub">
								<view class="text-lg text-bold text-cut">{{item.name}}</view>
								<view class="text-gray">{{item.code}}</view>
							</view>
							<view>序号:{{item.seq}}</view>
						</view>
					</tui-list-cell>
				</template>
			</tui-swipe-action>
		</tui-list-view>
		<u-loadmore :status="result.current < result.pages?'loading':'nomore'" />
		<coco-fixed-button @click.native="edit"></coco-fixed-button>
	</view>
</template>

<script>
	export default {
		onLoad: function(option) {
			if (option.parentId) {
				this.page.cond.parentId = option.parentId || null;
				this.parentId = option.parentId || null;
			}
			this.list();
		},
		onShow: function() {
			this.stickyEnable = true;
		},
		onHide: function() {
			this.stickyEnable = false;
		},
		onReady: function() {},
		onReachBottom: function() {
			if (this.result.current < this.result.pages) {
				this.page.current += 1;
				this.list();
			}
		},
		data() {
			return {
				stickyEnable: false, // 吸顶组件在页面隐藏时禁用
				actions: [{
					name: '修改',
					width: 70,
					fontsize: 30,
					color: '#fff',
					background: '#ffaa00'
				}, {
					name: '删除',
					color: '#fff',
					fontsize: 30, //单位rpx
					width: 70, //单位px
					background: '#ff5500'
				}], // 滑动菜单动作配置
				page: {
					current: 1,
					size: 20,
					total: 0,
					orders: [{
						column: "seq",
						asc: true
					}],
					cond: {},
				}, // 分页查询请求
				result: {
					current: 1,
					pages: 1,
					records: []
				}, // 分页查询结果
				parentId: null, // 父组织ID
			};
		},
		methods: {
			// 添加
			edit(item) {
				uni.navigateTo({
					url: `dict-edit?id=${item.id || ''}&parentId=${this.parentId || ''}`,
					events: {
						refresh: () => {
							this.page.current = 1;
							this.list();
						}
					}
				});
			},
			// 查询
			search() {
				this.page.current = 1;
				this.list();
			},
			// 分页查询
			list() {
				this.$http.post("/admin/sys/dict/list", this.page, res => {
					let result = res.data || this.result;
					if (this.page.current > 1) {
						result.records = this.result.records.concat(result.records);
					}
					this.result = result;
				}, false, false, true);
			},
			// 子组织
			childList(item) {
				uni.navigateTo({
					url: `dict-list?parentId=${item.id}`,
				});
			},
			// 删除
			remove(item) {
				this.$utils.modal("提示", "是否删除？", true, flag => {
					if (flag) {
						this.$http.post("/admin/sys/dict/remove", item, res => {
							let records = this.result.records || [];
							records.splice(records.findIndex(obj => obj.id === item.id), 1);
							this.result.records = records;
						}, true, true, true);
					}
				});
			},
			// 滑动菜单事件处理
			actionHandler(obj) {
				// obj.index, obj.item:params
				if (obj.index === 0) {
					this.edit(obj.item);
				} else if (obj.index === 1) {
					this.remove(obj.item);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>
